package com.ruoyi.system.service.vantservice.customerervice.impl;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.mapper.vant.login.ApiLoginMapper;
import com.ruoyi.system.service.vantservice.customerervice.ApiQRCodeService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * @Author: 刘锟
 * @Date: Created in 2021/3/9 14:45
 * @Description: 用户二维码
 * @Modified By:
 */
@Service
public class ApiQRCodeServiceImpl implements ApiQRCodeService {

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    HttpServletRequest request;

    @Autowired
    private ApiLoginMapper apiLoginMapper;

    /**
     * 生成用户二维码
     *
     * @return
     */
    @Override
    public String createQRCode() {

        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);

        CustomerLogin result = apiLoginMapper.selectCustomerByName(customerLogin.getLoginName());

        if (result.getQRCode() != null && !"".equals(result.getQRCode())) {
            return result.getQRCode();
        } else {
            String url = "/home/ruoyi/profile/xiazai/"; //图片存放地址
            File f = new File(url);
            if (f.exists()) {
                f.mkdirs();
            }
            String newFileNamePrefix = UUID.randomUUID().toString().replace("-", "") + new SimpleDateFormat("yyyyMMddHHssSSS").format(new Date());
            String newFileName = newFileNamePrefix + ".png";
            //获取用户id
            Long customerId = customerLogin.getCustomerId();

            final int width = 300;
            final int height = 300;
            final String format = "png";
            final String content = "http://51jhf.com/txj/#/login_index?inviterId=" + customerId;

            //定义二维码的参数
            HashMap hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 指定编码格式
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 指定纠错等级
            hints.put(EncodeHintType.MARGIN, 1);//设置白边
            //生成二维码
            try {
                //编码的内容,编码的方式（二维码、条形码...）,首选的宽度,首选的高度,编码时的额外参数
                BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
                Path file = new File(url + newFileName).toPath();
                MatrixToImageWriter.writeToPath(bitMatrix, format, file);
            } catch (Exception e) {
                return "500";
            }
            String QRCode = "http://51jhf.com/profile/xiazai/" + newFileName;
            customerLogin.setQRCode(QRCode);
            apiLoginMapper.updateCustomer(customerLogin);
            return QRCode;
        }
    }
}
